package com.yhsimon.lock;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock implements Runnable {

    public static void main(String[] args) {
        TestLock lock=new TestLock();
        new Thread(lock).start();
        new Thread(lock).start();
        new Thread(lock).start();
    }


    private  int tickets=10;
    private ReentrantLock lock=new ReentrantLock();//定义lock锁
    @Override
    public void run() {
        try {
            lock.lock(); //显示加锁
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(tickets>0){
                    System.out.println(tickets--);
                }else{
                    break;
                }
            }
        }finally {
            lock.unlock();
        }
    }
}
